package com.cjyd.hrjb.config;

import com.cjyd.hrjb.convert.EnumSerializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Created fei wo by 2020/4/7
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.stream().filter(httpMessageConverter -> httpMessageConverter instanceof MappingJackson2HttpMessageConverter).forEach(httpMessageConverter -> {
            MappingJackson2HttpMessageConverter converter = (MappingJackson2HttpMessageConverter) httpMessageConverter;
            EnumSerializer enumSerializer = new EnumSerializer();
            SimpleModule simpleModule = new SimpleModule();
            simpleModule.addSerializer(enumSerializer);
            converter.getObjectMapper().registerModule(simpleModule);
        });
    }
}
